//
//  ZWHNewTableViewCell.swift
//  约麻
//
//  Created by Syrena on 2017/7/20.
//  Copyright © 2017年 Sweet. All rights reserved.
//

import UIKit
import AVOSCloud
import Hyphenate

protocol ZWHNewTableViewCellDelegate {
    func addfriend(page:Int)
}

class ZWHNewTableViewCell: UITableViewCell {

    @IBOutlet weak var reviceB: UIButton!
    
    
    
    @IBOutlet weak var introL: UILabel!
    @IBOutlet weak var nameL: UILabel!
    @IBOutlet weak var iconV: UIImageView!
    
    var username:String?
    var page=0
    var delegate:ZWHNewTableViewCellDelegate?
    
    var dict:[String:String]?{
        didSet{
            username = dict?["username"]
            iconV.layer.cornerRadius = 5
            iconV.layer.masksToBounds = true
            let que = AVQuery(className: "_User")
            que.whereKey("username", equalTo: dict?["username"] ?? "")
            que.findObjectsInBackground { [weak self](objects, error) in
                let user = objects?.last as? AVUser
                if let name = user?.object(forKey: "name") as? String{
                    self?.nameL.text = name
                }else{
                    self?.nameL.text = user?.username
                }
                
                if let image = user?.object(forKey: "headimage") as? String{
                    self?.iconV.sd_setImage(with: URL(string: image))
                }else{
                    self?.iconV.image = UIImage(named: "茶叶")
                }
                self?.introL.text = self?.dict?["message"]
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBAction func revicefriend(_ sender: UIButton) {
        let error = EMClient.shared().contactManager.acceptInvitation(forUsername: username)
        if error == nil{
            delegate?.addfriend(page: page)
        }
    }
}
